package io.renren.com.qywt.excel.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import io.renren.com.qywt.dto.AftersaleInventoryDTO;
import io.renren.com.qywt.entity.AftersaleInventoryEntity;
import io.renren.com.qywt.service.AftersaleInventoryService;
import io.renren.com.qywt.service.TaskLocationFeeService;
import io.renren.commons.tools.utils.ConvertUtils;
import io.renren.commons.tools.utils.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class AftersaleInventoryExcelListener<E, T> extends AnalysisEventListener<T> {
    private static final Logger LOGGER = LoggerFactory.getLogger(AftersaleInventoryExcelListener.class);
    /**
     * 每隔2000条存储数据库，然后清理list，方便内存回收
     */
    private static final int BATCH_COUNT = 2000;
    private final List<AftersaleInventoryDTO> list = new ArrayList<>();

    /**
     * 通过构造器注入Service
     */
    private final TaskLocationFeeService taskLocationFeeService;
    private final AftersaleInventoryService aftersaleInventoryService;

    /**
     * 构造方法
     *
     * @param taskLocationFeeService 对象
     */
    public AftersaleInventoryExcelListener(TaskLocationFeeService taskLocationFeeService,AftersaleInventoryService aftersaleInventoryService) {
        this.taskLocationFeeService = taskLocationFeeService;
        this.aftersaleInventoryService = aftersaleInventoryService;
    }

    /**
     * 每条数据解析完，都会调用此方法
     */
    @Override
    public void invoke(T data, AnalysisContext context) {
        LOGGER.info("解析到一条数据:{}", JsonUtils.toJsonString(data));
        AftersaleInventoryDTO dto=ConvertUtils.sourceToTarget(data, AftersaleInventoryDTO.class);
        Integer num=taskLocationFeeService.getByProdCode(dto.getProductNo());
        if(num>0){
            dto.setExistFlag(1);
        }else{
            dto.setExistFlag(0);
        }
        list.add(dto);

        // 达到BATCH_COUNT了，需要去存储一次数据库，防止数据几万条数据在内存，容易OOM
        if (list.size() >= BATCH_COUNT) {
            saveData();
            // 存储完成清理 list
            list.clear();
        }
    }
    /**
     * 所有数据解析完成了 都会来调用
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // 这里也要保存数据，确保最后遗留的数据也存储到数据库
        saveData();
        LOGGER.info("所有数据解析完成！");
    }

    /**
     * 加上存储数据库
     */
    private void saveData() {
        LOGGER.info("{}条数据，开始存储数据库！", list.size());
        List<AftersaleInventoryEntity> entityList=ConvertUtils.sourceToTarget(list,AftersaleInventoryEntity.class);
        aftersaleInventoryService.insertBatch(entityList);
        LOGGER.info("存储数据库成功！");
    }
}
